﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler
{


    public class UpDataMenuCommendHandlers : IRequestHandler<UpDataMenuCommends, APIResult<Menu>>
    {
        private readonly IBaseRepository<Menu> menuRepository;
        private readonly IMapper mapper;

        public UpDataMenuCommendHandlers(IBaseRepository<Menu> menuRepository, IMapper mapper)
        {
            this.menuRepository = menuRepository;
            this.mapper = mapper;
        }



        /// <summary>
        /// 修改菜单数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Menu>> Handle(UpDataMenuCommends request, CancellationToken cancellationToken)
        {
            APIResult<Menu> result = new APIResult<Menu>();

            var menus = mapper.Map<Menu>(request);
            menus.UpdateName = request.UpdateName;
            menus.UpdateTime = DateTime.Now;


            var data= menuRepository.Update(menus).Result;

            if (data>0) {
               

                
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                

            }
        
         
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }

         

            return await Task.FromResult(result);
        }
    }
}
